package project.order.work;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSession;

import net.sf.json.JSONObject;

public class SendArticleThread extends Thread {
    private String openip;
    private String content ;
    
    public SendArticleThread(String openip,String content ) {
        this.openip=openip;
        this.content=content;
    }
    
    public void run() {
        System.out.println("Thread start ");
        String sJSON=this.createJSONObject(openip,content).toString();
        System.out.println("JSON="+sJSON);
        //send to 微信
        try {
            URL url=new URL("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token="+AccessTokenThread.access_token);
            
            trustAllHttpsCertificates();
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
            
            HttpURLConnection conn=(HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            
            OutputStream os=null;
            
            try {
                os=conn.getOutputStream();
                os.write(sJSON.getBytes(("UTF-8")));
                os.flush();
                os.close();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
    
            //is
            InputStream is=null;
            try {
                is=conn.getInputStream();
                String sReturn=new String(readInputStream(is),("UTF-8"));
                System.out.println("get winxin Message:"+sReturn);
                is.close();
                
                JSONObject json=JSONObject.fromObject(sReturn);
                System.out.println((Integer) json.get("errcode"));
                System.out.println((String) json.get("errmsg"));
                System.out.println("json:"+json.toString());
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
        
        System.out.println("Thread stop ");
    }
    // JSONObject
    public  JSONObject createJSONObject(String OPENID,String content) {  
        JSONObject jsonObject = new JSONObject();  
        jsonObject.put("touser", OPENID);  
        jsonObject.put("msgtype", "text");  
        JSONObject dataelem1=new JSONObject();
        
        JSONObject dataelem2=new JSONObject();//articles item
        dataelem2.put("content",content);  
       
        jsonObject.put("text",dataelem1);
          
        return jsonObject;  
    }  
    
    
    
    public byte[] readInputStream(InputStream inStream){
        byte[] data=null;
        try {
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while( (len = inStream.read(buffer)) !=-1 ){
                outStream.write(buffer, 0, len);
            }
            data = outStream.toByteArray();//
            outStream.close();
            inStream.close();

        }
        catch(Exception ex) {
            ex.printStackTrace();
        }
        return data;
    }
    
    
    
   //以下开始时https的信任默认代码
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            System.out.println("Warning: URL Host: " + urlHostName + " vs. "
                               + session.getPeerHost());
            return true;
        }
    };
 
     private static void trustAllHttpsCertificates() throws Exception {
         javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
         javax.net.ssl.TrustManager tm = new Mitm();
         trustAllCerts[0] = tm;
         javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");
         sc.init(null, trustAllCerts, null);
         javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
     }
    
     static class Mitm implements javax.net.ssl.TrustManager,javax.net.ssl.X509TrustManager {
         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
             return null;
         }
    
         public boolean isServerTrusted(java.security.cert.X509Certificate[] certs) {
             return true;
         }
    
         public boolean isClientTrusted(java.security.cert.X509Certificate[] certs) {
            return true;
         }
    
         public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
            return;
         }
    
         public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) throws java.security.cert.CertificateException {
             return;
         }
     }
}